package com.yunupmall.service.admin.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunupmall.api.fordVO.FordSpecsPo;
import com.yunupmall.api.fordVO.SpecsVo;
import com.yunupmall.api.fordentity.Specs;
import com.yunupmall.api.po.SpecsPo;
import com.yunupmall.mapper.commoditymapper.SpecsMapper;
import com.yunupmall.service.admin.service.SpecsService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class Specsimpl extends ServiceImpl<SpecsMapper,Specs> implements SpecsService {
  private   SpecsMapper specsMapper;

    @Override
    public int AddSpecsColor(String[] array, Integer kindid) {
        Map<String, Object> map = new HashMap<>();
        map.put("colorArray", array);
        map.put("kindid", kindid);
        return specsMapper.AddSpecsColor(map);
    }

    @Override
    @Transactional
    public String AddSpecs(List<SpecsVo> specsData) {
       if (ObjectUtil.isEmpty(specsData)){
           throw new RuntimeException("参数异常");
       }
        List<Specs> specsArrayList = new ArrayList<>();
        final Specs[] specs = {new Specs()};
        specsData.forEach(specsVo -> {
            specsVo.getTrendTableSpecs().getValue().forEach(value->{
                specs[0] =new Specs();
                specs[0].setSpecsValue(value);
                specs[0].setName(specsVo.getTrendTableSpecs().getName());
                specs[0].setKindId(specsVo.getKindId());
                specs[0].setCreateTime(LocalDateTime.now());
                specs[0].setDeflag(0);
                specsArrayList.add(specs[0]);
            });
       });
        if(this.saveBatch(specsArrayList)){
            return "成功";
        }else {
            throw new RuntimeException("失败");
        }
    }

    /**
     * 分页查询
     * @param fordSpecsPo
     * @return
     */
    @Override
    public List<SpecsPo> pageId(FordSpecsPo fordSpecsPo) {
        LambdaQueryWrapper<Specs> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(fordSpecsPo.getId())){
            wrapper.eq(Specs::getId,fordSpecsPo.getId());
        }
        if (ObjectUtil.isNotEmpty(fordSpecsPo.getKindId())){
            wrapper.eq(Specs ::getKindId,fordSpecsPo.getKindId());
        }
        wrapper.eq(Specs ::getDeflag,0);
        List<Specs> specsList = this.list(wrapper);
        List<SpecsPo> specsPoList = new ArrayList<>();
        if (specsList.size() > 0){
            Map<String, List<Specs>> collect = specsList.stream().collect(Collectors.groupingBy(specs -> specs.getName()));
           for(Map.Entry<String, List<Specs>> map : collect.entrySet() ){
               SpecsPo specsPo = new SpecsPo();
               StringBuilder stringBuilder = new StringBuilder();
               map.getValue().forEach(section -> {
                   stringBuilder.append(section.getSpecsValue()+"、");
               });
               specsPo.setName(map.getKey());
               specsPo.setValue(stringBuilder.toString());
               specsPo.setId(map.getValue().get(0).getId());
               specsPo.setCreateTime(map.getValue().get(0).getCreateTime());
               specsPoList.add(specsPo);
           }
        }
        return specsPoList;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    @Transactional
    public String deleteData(Integer id) {
        if (ObjectUtil.isEmpty(id)) {
            throw new RuntimeException("ID不能为空");
        }
        Specs specs = new Specs();
        specs.setId(id);
        specs.setDeflag(1);
        if (this.updateById(specs)){
            return "删除成功";
        }else {
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * byid
     * @param id
     * @return
     */
    @Override
    public List<SpecsPo> byid(Integer id) {
        if (ObjectUtil.isEmpty(id)) {
            throw new RuntimeException("类别ID为空");
        }
        LambdaQueryWrapper<Specs> specsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        specsLambdaQueryWrapper.eq(Specs ::getKindId,id);
        specsLambdaQueryWrapper.eq(Specs ::getDeflag,0);
        List<Specs> list = this.list(specsLambdaQueryWrapper);
        List<SpecsPo> specsPoList = new ArrayList<>();
        if (list.size() > 0){
            Map<String, List<Specs>> collect = list.stream().collect(Collectors.groupingBy(specs -> specs.getName()));
            for(Map.Entry<String, List<Specs>> map : collect.entrySet() ){
                SpecsPo specsPo = new SpecsPo();
                StringBuilder stringBuilder = new StringBuilder();
                map.getValue().forEach(section -> {
                    stringBuilder.append(section.getSpecsValue()+"、");
                });
                specsPo.setName(map.getKey());
                specsPo.setValue(stringBuilder.toString());
                specsPo.setId(map.getValue().get(0).getId());
                specsPo.setCreateTime(map.getValue().get(0).getCreateTime());
                specsPoList.add(specsPo);
            }
        }
        return specsPoList;
    }

    /**
     * 获得未处理数据集合
     * @param specs
     * @return
     */
    @Override
    public List<Specs> getList(Specs specs) {
        if (ObjectUtil.isEmpty(specs)){
            throw new RuntimeException("参数异常");
        }
        LambdaQueryWrapper<Specs> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(specs.getName())) {
            wrapper.eq(Specs ::getName,specs.getName());
        }
        if (ObjectUtil.isNotEmpty(specs.getKindId())) {
            wrapper.eq(Specs ::getKindId,specs.getKindId());
        }
        wrapper.eq(Specs ::getDeflag,0);
        return this.list(wrapper);
    }
}
